<template>
	<!--选择标的责任-->
	<el-dialog title="选择标的/责任" :visible.sync="dialogVisible" size="small" :close-on-click-modal="false" :close-on-press-escape="false" :before-close="off">
		  <el-form label-width="150px" >
		  <el-row :span="24">
		  	<el-form-item label="当前条款对应责任" label-width="150px" :span="24">
				<el-select v-model="arrayItems" multiple filterable readonly value-key="key" size="small" style="width:80%">
					<el-option v-for="item in itemOptions" :key="item.itemcode" :label="item.kindcode + '-' + item.itemcode + '-' + item.itemcname" :value="item"></el-option>
				</el-select>
			</el-form-item>
		  </el-row>
		</el-form>
		  <span slot="footer" class="dialog-footer">
			   <el-button @click="off">取 消</el-button>
			   <el-button type="danger" @click="reset">清 空</el-button>
			   <el-button type="warning" @click="select">确 定</el-button>
		  </span>
	</el-dialog>
</template>

<script>

import PlanAPI from "../../../../js/production/plan/planAPI";

  export default {
	data() {
		return {
			dialogVisible: true,
			itemOptions: [],
			risk: null,
			kinds : [],
			arrayItems : [],
		}
	},
	mounted() {
		this.risk = this.$parent.formData.risk;
		this.kinds = this.$parent.formData.kinds;
		PlanAPI.getItemOptions(this);
		this.arrayItems = this.$parent.formData.items;
	},
	methods: {
		select() {
			if (this.$parent.formData.kinditems.length > 0) {
				this.$parent.cacheKindItems = Object.assign([], this.$parent.formData.kinditems);
			}

			this.$parent.formData.itemcodes = '';
			this.$parent.formData.items = [];
			this.$parent.formData.kinditems = [];
			this.$parent.formData.items = this.arrayItems;
			for(let i = 0; i <this.arrayItems.length - 1; i++) {
				this.$parent.formData.itemcodes += this.arrayItems[i].itemcode + ",";
			}
			if(this.arrayItems.length > 0) {
				this.$parent.formData.itemcodes+= this.arrayItems[this.arrayItems.length - 1].itemcode;
			}

			/*********************************************************************************************************************/
			//封装kinditem数据
			let kinds = this.$parent.formData.kinds;
			for(let i = 0; i < kinds.length; i++) {
				let items = this.$parent.formData.items;
				let hasItems = false;
				if (items.length > 0) {
					for(let j = 0; j <items.length; j++) {
						if (kinds[i].kindcode === items[j].kindcode && kinds[i].kindversion === items[j].kindversion) {
							hasItems = true;
							this.$parent.formData.kinditems.push({
								kindcode: kinds[i].kindcode,
								kindversion: kinds[i].kindversion,
								kindcname: kinds[i].kindcname,
								kindename: kinds[i].kindename,
								itemcode: items[j].itemcode,
								itemcname: items[j].itemcname,
								itemename: items[j].itemename,
								itemflag: items[j].itemflag,
								quantity : '1',
								currency : 'CNY',
								discount : '100.00000000',
								amount : '0.00',
								unitamount : '0.00',
								premium : '0.00',
								unitpremium : '0.00',
								rate: '0.00000000',
								calculateflag : kinds[i].calculateflag.substring(0,1),
								ownerFlag : '2'
							})
						} 
					}
				}
				if (!hasItems) {
					this.$parent.formData.kinditems.push({
						kindcode: kinds[i].kindcode,
						kindversion: kinds[i].kindversion,
						kindcname: kinds[i].kindcname,
						kindename: kinds[i].kindename,
						itemcode: null,
						quantity : '1',
						currency : 'CNY',
						discount : '100.00000000',
						amount : '0.00',
						unitamount : '0.00',
						premium : '0.00',
						unitpremium : '0.00',
						rate: '0.00000000',
						calculateflag : kinds[i].calculateflag.substring(0,1),
						ownerFlag : '2'
					})
				}
			}

			/*********************************************************************************************************************/
			//更新kinditems记录, 替换为缓存cacheKindItems中与之匹配的数据
			let kinditems = this.$parent.formData.kinditems;
			let cacheKindItems = this.$parent.cacheKindItems;
			for (let i = 0; i < kinditems.length; i++) {
				for (let j = 0; j < cacheKindItems.length; j++) {
					if(kinditems[i].kindcode === cacheKindItems[j].kindcode && kinditems[i].itemcode === cacheKindItems[j].itemcode) {
						kinditems[i] = cacheKindItems[j];
						break;
					}
				}
			}
			this.off();
		},
		reset() {
			this.arrayItems = [];
			this.select();
		},
		off() {
			this.$parent.showItemCodeSelectDialog = false;
		},
	}
  }
</script>